package com.bustle.ejb.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.bustle.ejb.entity.Product;
import com.bustle.ejb.entity.Product.ProductType;
import com.bustle.ejb.entity.Provider;
import com.bustle.ejb.service.dao.ProductDao;

@Local
public interface ProductManager {
	
	public List<Product> getListProduct();
	public Product getById(int idProduct);
	public List<ProductType> getProductType();
	public List<Product> getSearchProductTypeProvider(ProductType productType, Integer idProvider);
	public List<Product> getSearchProductProductType(ProductType productType);
	public List<Product> getSearchProductProvider(int idProvider);
	
	@Stateless
	public class DefaultProductManager implements ProductManager{
		@PersistenceContext
	    protected EntityManager entityManager;
		
		@EJB
		private ProductDao productDao;

		@Override
		public List<Product> getListProduct() {
			return productDao.retrieveAll();
		}

		@Override
		public List<Product> getSearchProductTypeProvider(ProductType productType, Integer idProvider) {
			String paramQuery = "SELECT p FROM Product p WHERE p.productType = :paramProductType AND p.provider.idProvider = :paramIdProvider";
			Query query = entityManager.createQuery(paramQuery);
			query.setParameter("paramProductType", productType);
			query.setParameter("paramIdProvider", idProvider);
			List<Product> result = query.getResultList();
			System.out.println("longueur de la liste " + result.size());
			return result;
		}

		@Override
		public List<Product> getSearchProductProductType(ProductType productType) {
			String paramQuery = "SELECT p FROM Product p WHERE p.productType = :paramProductType";
			Query query = entityManager.createQuery(paramQuery);
			query.setParameter("paramProductType", productType);
			List<Product> result = query.getResultList();
			System.out.println("longueur de la liste " + result.size());
			return result;
		}

		@Override
		public List<Product> getSearchProductProvider(int idProvider) {
			String paramQuery = "SELECT p FROM Product p WHERE p.provider.idProvider = :paramIdProvider";
			Query query = entityManager.createQuery(paramQuery);
			query.setParameter("paramIdProvider", idProvider);
			List<Product> result = query.getResultList();
			System.out.println("longueur de la liste " + result.size());
			return result;
		}

		@Override
		public Product getById(int idProduct) {
			return productDao.retrieve(idProduct);
		}

		@Override
		public List<ProductType> getProductType() {
			ProductType[] productTypes = ProductType.values();
			List<ProductType> result = new ArrayList<ProductType>();
			for(ProductType pt : productTypes)
				result.add(pt);
			return result;
		}

		public DefaultProductManager() {
		}

		
		
		
	}

}
